home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / vector / BitVec.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  7KB  |  260 lines

  1. #ifndef    BITVEC_H
  2. #define    BITVEC_H
  3.  
  4. /* BitVec.h -- Bit Vectors
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.     K. E. Gorlen
  15.     Bg. 12A, Rm. 2033
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-1111
  21.     uucp: uunet!nih-csl!kgorlen
  22.     Internet:kgorlen@alw.nih.gov
  23.  
  24. Modification History:
  25.  
  26. $Log:    BitVec.h,v $
  27.  * Revision 3.0  90/05/16  23:00:22  kgorlen
  28.  * Release for 1st edition.
  29.  * 
  30. */
  31.  
  32. #include "Vector.h"
  33.  
  34. typedef unsigned char bitVecByte;
  35.  
  36. class BitSlice;
  37. class BitPick;
  38. class BitSlct;
  39. class BitSliceIstream;
  40. class BitSliceOstream;
  41. class BitPickIstream;
  42. class BitPickOstream;
  43.  
  44. class BitRef : public NIHCL {
  45.     bitVecByte* p;    // pointer to byte containing bit
  46.     bitVecByte m;    // mask for bit
  47.     BitRef(bitVecByte* v, int i) {
  48.         p = v + (i>>3);
  49.         m = 1 << (i&7);
  50.     }
  51.     BitRef(const BitRef& b)    { p = b.p; m = b.m; }
  52.     friend BitVec;
  53. public:
  54.     operator bool() const    { return ((*p & m) != 0); }
  55.     bool operator=(bool b) {
  56.         if (b) *p |= m;
  57.         else *p &= ~m;
  58.         return b;
  59.     }
  60.     void operator&=(bool b) { if (!b) *p &= ~m; }
  61.     void operator|=(bool b) { if (b) *p |= m; }
  62.     void operator^=(bool b) { if (b) *p ^= m; }
  63. };
  64.  
  65. class BitVec : public Vector {
  66.     DECLARE_MEMBERS(BitVec);
  67.     bitVecByte* v;    // pointer to data, NULL if empty vector
  68.     void indexRangeErr() const;
  69. protected:
  70.     virtual void storer(OIOofd&) const;
  71.     virtual void storer(OIOout&) const;
  72. public:
  73.     BitVec(unsigned len =0);
  74.     BitVec(unsigned len, bool init);
  75.     BitVec(const bitVecByte*, unsigned len);
  76.     BitVec(const BitVec&);
  77.     BitVec(const BitSlice&);
  78.     ~BitVec()            { delete v; }
  79.     BitSlice operator()(int pos, unsigned lgt, int stride =1);
  80.     const BitSlice operator()(int pos, unsigned lgt, int stride =1) const;
  81.     bitVecByte* pt()        { return v; }
  82.     const bitVecByte* pt() const    { return v; }
  83.     operator BitSlice();
  84.     operator const BitSlice() const;
  85.     BitRef operator[](int i);
  86.     BitRef operator()(int i)        { return BitRef(v,i); }
  87.     const BitRef operator[](int i) const;
  88.     const BitRef operator()(int i) const    { return BitRef(v,i); }
  89.     BitPick    operator[](const IntVec&);
  90.     const BitPick operator[](const IntVec&) const;
  91.     BitSlct    operator[](const BitVec&);
  92.     const BitSlct operator[](const BitVec&) const;
  93.     void /*BitVec::*/operator=(const BitVec&);
  94.     void /*BitVec::*/operator=(const BitSlice&);
  95.     void /*BitVec::*/operator=(const BitSlct&);
  96.     void /*BitVec::*/operator=(const BitPick&);
  97.     void /*BitVec::*/operator=(bool);
  98.     unsigned nbytes() const    { return (n+7) >> 3; }
  99.     void /*BitVec::*/lengthErr(const BitSlice&) const;
  100.     void selectErr(const BitVec&) const;
  101. friend    BitVec    operator!(const BitVec&);
  102. friend    BitVec    operator&(const BitVec&,const BitVec&);
  103. friend    BitVec    operator^(const BitVec&,const BitVec&);
  104. friend    BitVec    operator|(const BitVec&,const BitVec&);
  105. friend    void    operator&=(BitVec&,const BitVec&);
  106. friend    void    operator^=(BitVec&,const BitVec&);
  107. friend    void    operator|=(BitVec&,const BitVec&);
  108. friend    int    sum(const BitVec&);
  109.     virtual void deepenShallowCopy();
  110.     virtual unsigned hash() const;
  111.     virtual bool isEqual(const Object&) const;
  112.     virtual void printOn(ostream& strm =cout) const;
  113.     virtual void scanFrom(istream& strm);
  114.     virtual const Class* species() const;
  115. };
  116.  
  117. inline BitRef BitVec::operator[](int i)
  118. {
  119.     if ((unsigned)i >= n) indexRangeErr();
  120.     return BitRef(v,i);
  121. }
  122.     
  123. inline const BitRef BitVec::operator[](int i) const
  124. {
  125.     if ((unsigned)i >= n) indexRangeErr();
  126.     return BitRef(v,i);
  127. }
  128.     
  129. class TempBitVec : public BitVec {
  130.     friend BitSlice;
  131.     friend BitPick;
  132.     friend BitSlct;
  133.     TempBitVec(unsigned len =0) : BitVec(len) {}
  134.     virtual void free() { delete this; }
  135. };
  136.  
  137. class BitSlice : public NIHCL {
  138.     BitVec*    V;    // vector pointer
  139.     int p;        // slice bit number
  140.     unsigned l;    // slice length
  141.     int k;        // slice stride
  142.     BitSlice(const BitVec& v, int pos, unsigned lgt, int stride =1);
  143.     BitSlice(const BitVec& v, unsigned lgt) {
  144.         V = &(BitVec&)v;  p = 0;  l = lgt;  k = 1;
  145.     }
  146.     BitSlice(const BitSlice&);
  147.     friend BitVec;
  148.     friend BitSliceIstream;
  149.     friend BitSliceOstream;
  150. public:
  151.     BitSlice(const BitPick&);
  152.     BitSlice(const BitSlct&);
  153.     ~BitSlice()        { V->free(); }
  154.     int pos() const        { return p; }
  155.     unsigned length() const    { return l; }
  156.     int stride() const    { return k; }
  157.     void /*BitSlice::*/operator=(const BitVec&);
  158.     void /*BitSlice::*/operator=(const BitPick&);
  159.     void /*BitSlice::*/operator=(const BitSlct&);
  160.     void /*BitSlice::*/operator=(const BitSlice&);
  161.     void /*BitSlice::*/operator=(bool);
  162.     void /*BitSlice::*/lengthErr(const BitVec&) const;
  163.     void /*BitSlice::*/lengthErr(const BitSlice&) const;
  164.     void /*BitSlice::*/lengthErr(const IntVec&) const;
  165.     void selectErr(const BitVec&) const;
  166. friend    BitVec    operator!(const BitSlice&);
  167. friend    BitVec    operator&(const BitSlice&,const BitSlice&);
  168. friend    BitVec    operator^(const BitSlice&,const BitSlice&);
  169. friend    BitVec    operator|(const BitSlice&,const BitSlice&);
  170. friend    void    operator&=(BitSlice&,const BitSlice&);
  171. friend    void    operator^=(BitSlice&,const BitSlice&);
  172. friend    void    operator|=(BitSlice&,const BitSlice&);
  173. friend    BitVec    reverse(const BitSlice&);
  174. friend    int    sum(const BitSlice&);
  175. };
  176.  
  177. class BitPick : public NIHCL {
  178.     BitVec* V;
  179.     const IntVec* X;
  180.     BitPick(const BitVec& v,const IntVec& x)    { V = &(BitVec&)v;  X = &x; }
  181.     BitPick(const BitPick& s)            { V = s.V; X = s.X; }
  182.     friend BitVec;
  183.     friend BitSlice;
  184.     friend BitSlct;
  185.     friend BitPickIstream;
  186.     friend BitPickOstream;
  187. public:
  188.     void /*BitPick::*/operator=(const BitVec&);
  189.     void /*BitPick::*/operator=(const BitPick&);
  190.     void /*BitPick::*/operator=(const BitSlct&);
  191.     void /*BitPick::*/operator=(const BitSlice&);
  192.     void /*BitPick::*/operator=(bool);
  193.     unsigned length() const;
  194. };
  195.  
  196. class BitSlct : public NIHCL {
  197.     BitVec* V;
  198.     const BitVec* B;
  199.     BitSlct(const BitVec& v, const BitVec& b)   { V = &(BitVec&)v;  B = &b; }
  200.     BitSlct(const BitSlct& s)            { V = s.V; B = s.B; }
  201.     friend BitVec;
  202.     friend BitSlice;
  203.     friend BitPick;
  204. public:
  205.     void /*BitSlct::*/operator=(const BitVec&);
  206.     void /*BitSlct::*/operator=(const BitPick&);
  207.     void /*BitSlct::*/operator=(const BitSlct&);
  208.     void /*BitSlct::*/operator=(const BitSlice&);
  209.     void /*BitSlct::*/operator=(bool);
  210.     unsigned length() const    { return B->length(); }
  211. };
  212.  
  213. inline BitSlice BitVec::operator()(int pos, unsigned lgt, int stride)
  214. {
  215.     BitSlice s(*this,pos,lgt,stride);
  216.     return s;
  217. }
  218.  
  219. inline const BitSlice BitVec::operator()(int pos, unsigned lgt, int stride) const
  220. {
  221.     const BitSlice s(*this,pos,lgt,stride);
  222.     return s;
  223. }
  224.  
  225. inline BitVec::operator BitSlice()
  226. {
  227.     BitSlice s(*this,length());
  228.     return s;
  229. }
  230.  
  231. inline BitVec::operator const BitSlice() const
  232. {
  233.     const BitSlice s(*this,length());
  234.     return s;
  235. }
  236.  
  237. inline BitPick BitVec::operator[](const IntVec& I)
  238. {
  239.     return BitPick(*this,I);
  240. }
  241.  
  242. inline const BitPick BitVec::operator[](const IntVec& I) const
  243. {
  244.     const BitPick t(*this,I);
  245.     return t;
  246. }
  247.  
  248. inline BitSlct BitVec::operator[](const BitVec& B)
  249. {
  250.     return BitSlct(*this,B);
  251. }
  252.  
  253. inline const BitSlct BitVec::operator[](const BitVec& B) const
  254. {
  255.     const BitSlct t(*this,B);
  256.     return t;
  257. }
  258.  
  259. #endif
  260.